/*-----------------------------------------------------------------------------
 * file:  sr_icmp.h 
 * date:  Thu Oct 18, 2012  
 * Author: yujiali@cs.toronto.edu
 *
 * Description:
 *
 * Methods and datastructures for handeling the ICMP messages
 *---------------------------------------------------------------------------*/

#ifndef sr_ICMP_H
#define sr_ICMP_H

#include <sys/types.h>

struct sr_instance;

struct sr_icmp_hdr
{
    uint8_t type;
    uint8_t code;
    uint16_t checksum;
    uint32_t unused;        /* Can be reused with masks as other fields */
#define ICMP_ID_MASK        0xffff0000  /* Identifier mask */
#define ICMP_SEQNUM_MASK    0x0000ffff  /* Sequence number mask */
} __attribute__((packed));

/* define ICMP message types */
#define ICMP_TYPE_ECHO_REPLY        0
#define ICMP_TYPE_DST_UNREACHABLE   3
#define ICMP_TYPE_SRC_QUENCH        4
#define ICMP_TYPE_REDIRECT          5
#define ICMP_TYPE_ECHO              8
#define ICMP_TYPE_TIME_EXCEEDED     11
#define ICMP_TYPE_PARAMETER_PROB    12
#define ICMP_TYPE_TIMESTAMP         13
#define ICMP_TYPE_TIMESTAMP_REPLY   14
#define ICMP_TYPE_INFO_REQUEST      15
#define ICMP_TYPE_INFO_REPLY        16

#define ICMP_CODE_NET_UNREACHABLE       0
#define ICMP_CODE_HOST_UNREACHABLE      1
#define ICMP_CODE_PROTOCOL_UNREACHABLE  2
#define ICMP_CODE_PORT_UNREACHABLE      3
#define ICMP_CODE_DF                    4
#define ICMP_CODE_SOURCE_ROUTE_FAILED   5

#define ICMP_CODE_TIME_EXCEEDED_IN_TRANSIT      0
#define ICMP_CODE_TIME_EXCEEDED_IN_REASSEMBLY   1

/* usually 64 bits (8 bytes) of the original IP datagram is included
 * in an ICMP message. */
#define ICMP_IP_DATAGRAM_LEN    8

#define ICMP_DEFAULT_TTL    64

int sr_handle_icmp(struct sr_instance*, uint8_t*, unsigned int);

int sr_send_icmp_packet(struct sr_instance* sr, uint8_t type, uint8_t code,
        uint32_t ip_dst, uint32_t ip_src, uint8_t* recv_ip_buf);
#endif

